//
//  WebViewController.m
//  ChooseGift
//
//  Created by lanou on 15/12/17.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "WebViewController.h"
#import "PopoverView.h"
#import "RepliedTableViewController.h"
#import "ChooseGift.h"
#import "MainClassDetail.h"



#import "UMSocial.h"
#import "UMSocialQQHandler.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialSinaHandler.h"

@interface WebViewController ()<UIWebViewDelegate,UMSocialUIDelegate>
@property (nonatomic,strong) NSMutableArray *modelarray;
@property (nonatomic,strong) NSMutableArray *giftarray;
@property (nonatomic,strong) NSMutableArray *strategyarray;

@property (nonatomic,strong) NSMutableArray *titlearray;
@property (nonatomic,strong) NSMutableArray *imagearray;


@property (nonatomic,strong) NSString *string;
@property (nonatomic,strong) UIAlertView *alertview1;

@property (nonatomic,strong) UIAlertView *alertview2;
@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"礼物详情";
    self.view.backgroundColor = [UIColor whiteColor];
    UIWebView *webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, -100, kWidth,self.view.frame.size.height + 100)];
    if (self.type == 1) {
        NSString *string = [NSString stringWithFormat:kSearchDid,self.userid];
        NSLog(@"%@",string);
        NSURL *url = [NSURL URLWithString:string];
        
        [webview loadRequest:[NSURLRequest requestWithURL:url]];
    }else if (self.type == 2){
        NSURL *url = [NSURL URLWithString:self.userid];
        [webview loadRequest:[NSURLRequest requestWithURL:url]];
    }
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, -500, kWidth, 600)];
    view.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1];
    [webview.scrollView addSubview:view];
    webview.delegate = self;
    [self.view addSubview:webview];
    if ((self.style == 100)) {
        self.navigationItem.rightBarButtonItem = nil;
    }else{
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"选项" style:(UIBarButtonItemStylePlain) target:self action:@selector(barbutton:)];
    }
    
    [UMSocialData setAppKey:@"567a696c67e58ed02800246d"];
    
    [UMSocialQQHandler setQQWithAppId:@"1104976359" appKey:@"26TbNepAin7TcuoN" url:self.userid];
    [UMSocialQQHandler setSupportWebView:YES];
    
    [UMSocialWechatHandler setWXAppId:@"wx8d3230c521c7d0eb" appSecret:@"94e1d194bd17761ad082782d19a7959a" url:self.userid];
    
    [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    
    
}



-(void)doTime

{
    [_alertview1  dismissWithClickedButtonIndex:0 animated:NO];
    [_alertview2 dismissWithClickedButtonIndex:0 animated:NO];
    
}

- (void)barbutton:(UIButton *)sender{
    CGPoint point = CGPointMake(kWidth -  kWidth / 10,50);
    NSArray *titles = @[@"收藏", @"评论", @"分享"];
    NSArray *images = @[@"a18.png", @"13.png", @"pengyouquan.png"];
    
    PopoverView *pop = [[PopoverView alloc] initWithPoint:point titles:titles images:images];
    _alertview1 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:0, nil];
    _alertview2 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该礼物已被收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:0, nil];
    pop.selectRowAtIndex = ^(NSInteger index){
        NSTimer *timer;
        timer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(doTime) userInfo:nil repeats:NO];
        if (index == 0) {
            if (self.type == 1) {
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                NSData *data1 = [defaults objectForKey:@"gift"];
               
                if (data1 == nil) {
                    self.giftarray = [NSMutableArray array];
                    [self.giftarray addObject:self.choosegift];
                    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_giftarray];
                  
                    [defaults setObject:data forKey:@"gift"];
                    [_alertview1 show];
                    
                }else{
                    self.giftarray = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data1];
                    
                    if ([self contain:self.giftarray]) {
                        [_alertview2 show];
                        
                    } else{
                        self.giftarray = [NSMutableArray array];
                        
                        NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
                        
                        NSData *data1  = [defaults1 objectForKey:@"gift"];
                        self.giftarray  = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data1];
                        
                        [self.giftarray addObject:_choosegift];
                        
                        
                        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_giftarray];
                        [defaults setObject:data forKey:@"gift"];
                                [_alertview1 show];
                    }
                }

            }else if (self.type == 2){
#pragma mark--------攻略---------
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                NSData *data1 = [defaults objectForKey:@"id"];
                NSData *data2 = [defaults objectForKey:@"title"];
                NSData *data3 = [defaults objectForKey:@"image"];

                if (data1 == nil) {
                    self.strategyarray = [NSMutableArray array];
                    self.titlearray = [NSMutableArray array];
                    self.imagearray = [NSMutableArray array];
                    [self.strategyarray addObject:self.mainClassDetail.ID];
                    [self.titlearray addObject:self.mainClassDetail.title];
                    [self.imagearray addObject:self.mainClassDetail.cover_image_url];
                    NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:self.strategyarray];
                     NSData *data2 = [NSKeyedArchiver archivedDataWithRootObject:self.titlearray]; NSData *data3 = [NSKeyedArchiver archivedDataWithRootObject:self.imagearray];
                    
                    
                    
                    [defaults setObject:data1 forKey:@"id"];
                    [defaults setObject:data2 forKey:@"title"];
                    [defaults setObject:data3 forKey:@"image"];

                    [_alertview1 show];
                    
                }else{

                   self.strategyarray = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data1];
                   self.titlearray = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data2];
                   self.imagearray = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data3];
                    if ([self contain2:self.strategyarray]) {
                        [_alertview2 show];
                        
                    } else{
                        self.strategyarray = [NSMutableArray array];
                        self.titlearray = [NSMutableArray array];
                        self.imagearray = [NSMutableArray array];

                        NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
                        
                         NSData *data1  = [defaults1 objectForKey:@"id"];
                         NSData *data2  = [defaults1 objectForKey:@"title"];
                         NSData *data3  = [defaults1 objectForKey:@"image"];
                        self.strategyarray  = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data1];
                        self.titlearray  = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data2];
                        self.imagearray  = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data3];
                        [self.strategyarray addObject:_mainClassDetail.ID];
                        [self.titlearray addObject:_mainClassDetail.title];
                        [self.imagearray addObject:_mainClassDetail.cover_image_url];
                        NSData *data4 = [NSKeyedArchiver archivedDataWithRootObject:_strategyarray];
                        NSData *data5 = [NSKeyedArchiver archivedDataWithRootObject:_titlearray];
                        NSData *data6 = [NSKeyedArchiver archivedDataWithRootObject:_imagearray];
                        
                        
                        [defaults setObject:data4 forKey:@"id"];
                        [defaults setObject:data5 forKey:@"title"];
                        [defaults setObject:data6 forKey:@"image"];                        [_alertview1 show];
                    }
                }
                
                
                
            }
        }
        if (index == 1) {
            
            RepliedTableViewController *rep = [[RepliedTableViewController alloc]init];
            rep.ID = self.userid;
            [self.navigationController pushViewController:rep animated:YES];
            
            
        }
        if (index == 2) {
            NSURL *url = [NSURL URLWithString:self.imageStr];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *imageS = [UIImage imageWithData:data];
            
            
            [UMSocialSnsService presentSnsIconSheetView:self appKey:@"567a696c67e58ed02800246d" shareText:self.titleS shareImage:imageS shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline, nil] delegate:self];
        }
    };
    [pop show];
    
    
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL result = [UMSocialSnsService handleOpenURL:url];
    if (result == FALSE) {
        //调用其他SDK，例如支付宝SDK等
    }
    return result;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return  [UMSocialSnsService handleOpenURL:url];
}


- (BOOL)contain:(NSMutableArray *)array{
    
    for (ChooseGift *choosegift in array) {
        if ([choosegift.name isEqualToString:self.choosegift.name]) {
            return YES;
            
        }
    }
    return NO;
    
}

- (BOOL)contain2:(NSMutableArray *)array{
    
//    for (MainClassDetail *mainClass in array) {
//        if ([mainClass.ID isEqualToString:self.strategyarray]) {
//            return YES;
//            
//        }
//    }
//    return NO;
    //MainClassDetail *main = [[MainClassDetail alloc]init];
    for (int i = 0; i < self.titlearray.count; i ++) {
        if ([_mainClassDetail.title isEqualToString:self.titlearray[i]]) {
            return YES;
        }
    }
    return NO;
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//- (void)webViewDidFinishLoad:(UIWebView *)webView
//{
//    NSString *js1 = @"var div = document.getElementsByTagName('div')[5];";
//    
//    NSString *js2 = @"div.parentNode.removeChild(div);";
//    
//    [webView stringByEvaluatingJavaScriptFromString:js1];
//    [webView stringByEvaluatingJavaScriptFromString:js2];
//    
//    NSString *js3 = @"var div = document.getElementsByTagName('div')[7];";
//    
//    NSString *js4 = @"div.parentNode.removeChild(div);";
//    
//    [webView stringByEvaluatingJavaScriptFromString:js3];
//    [webView stringByEvaluatingJavaScriptFromString:js4];
//    
//    NSString *js5 = @"var div = document.getElementsByTagName('a')[5];";
//    
//    NSString *js6 = @"div.parentNode.removeChild(div);";
//    
//    [webView stringByEvaluatingJavaScriptFromString:js5];
//    [webView stringByEvaluatingJavaScriptFromString:js6];
//    
//    NSString *js7 = @"var div = document.getElementsByTagName('a')[0];";
//    
//    NSString *js8 = @"div.parentNode.removeChild(div);";
//    
//    [webView stringByEvaluatingJavaScriptFromString:js7];
//    [webView stringByEvaluatingJavaScriptFromString:js8];
//    
//    NSString *js9 = @"var div = document.getElementsByTagName('a')[4];";
//    
//    NSString *js10 = @"div.parentNode.removeChild(div);";
//    
//    [webView stringByEvaluatingJavaScriptFromString:js9];
//    [webView stringByEvaluatingJavaScriptFromString:js10];
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
